<template>
  <div>
    <el-form
      :inline="true"
      :model="formData"
      size="small"
      class="demo-form-inline"
    >
      <el-form-item prop="address" label="账号">
        <el-input
          v-model="formData.address"
          clearable
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="address" label="账号" width="380">
      </el-table-column>
      <el-table-column prop="amount" label="数量" width="140">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="90">
        <template slot-scope="scope">
          {{ scope.row.status | formatStatus }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="backBlockHash"
        label="backBlockHash"
        width="530"
      ></el-table-column>
      <el-table-column prop="blockHash" label="blockHash" width="530">
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="updateEthMass(scope.row.id)"
            :disabled="scope.row.status === 2"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <v-pagination :formData="formData" @load="getData"></v-pagination>
  </div>
</template>
<script>
import { queryEthMass, updateEthMass } from "@/api/MetaMaskManagement";
import pagination from "@/components/pagination";
export default {
  components: {
    vPagination: pagination,
  },
  data() {
    return {
      loading: true,
      formData: {
        address: "",
        blockHash: "",
        backBlockHash: "",
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
    };
  },
  filters: {
    formatStatus(state) {
      switch (state) {
        case 1:
          return "待赎回";
          break;

        default:
          return "已完成";
          break;
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    onSubmit() {
      this.formData.pageNo = 1;
      this.formData.pageSize = 10;
      this.formData.total = 0;
      this.getData();
    },
    getData() {
      this.loading = true;
      queryEthMass(this.formData).then((res) => {
        this.loading = false;
        this.tableData = res.data.resultData.list;
        this.formData.total = res.data.resultData.count;
      });
    },
    updateEthMass(id) {
      this.$msgbox({
        title: "",
        message: "是否确定修改状态",
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            const formData = {
              id: id,
              status: 2,
            };
            updateEthMass(formData).then((res) => {
              instance.confirmButtonLoading = false;
              if (res.data.errorCode === 200) {
                this.getData();
                done();
                this.$message.success(res.data.errorMessage || "修改成功");
              }
            });
          } else {
            done();
          }
        },
      });
    },
  },
};
</script>